package web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.DBtools;
import com.MD5;

/**
 * Servlet implementation class TeaOperate
 */
@WebServlet(description = "对教师名单进行维护", urlPatterns = { "/TeaOperate","/TeaDel","/resetpwd"})
public class TeaOperate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeaOperate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//接收参数，删除项的教师编号
		request.setCharacterEncoding("utf-8");//编码
		String TeaId = request.getParameter("TeaId");
		String  btn =request.getParameter("btn");
		System.out.println("btn"+btn);
		String sql="";
		String sql2="";
		
		 if("重置密码".equals(btn)) {
			
			sql="update user set UserPwd='"+MD5.md5("123456")+"' where UserNum ='"+TeaId+"'";					
			
		}else {
			
			//构造写入数据库的SQL
			 sql = "delete from teacher where TeaId=" +TeaId;
			 sql2 = "delete from user where UserNum=" +TeaId;
		}
		
		System.out.println("sql=" + sql);
		int r=0;
		int r2=0;
		try {
			if(sql.length()>0) { r = new DBtools().execute(sql);}
			
			if(sql2.length()>0) { r2 = new DBtools().execute(sql2);}
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("影响行数：" + r);

		//返回页面
		response.sendRedirect("depcontacts/tealist.jsp");

		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 处理学生表单中的数据项，新增，修改
				//接收用户数据
				request.setCharacterEncoding("utf-8");
				String TeaId = request.getParameter("TeaId");
				String TeaName = request.getParameter("TeaName");
				String TeaGender = request.getParameter("TeaGender");
				String TeaEmail = request.getParameter("TeaEmail");
				String TeaAge = request.getParameter("TeaAge");
				String DepName = request.getParameter("DepName");

				String  btn =request.getParameter("btn");//获取选择性信息，判断时执行更新还是删除操作

				//构造写入数据库sql
				String sql="";
				String sql2="";
				String num="";
				if("提交".equals(btn)){
					//添加数据
					try {
						ResultSet rs = new DBtools().query("select * from teacher where TeaId="+TeaId);
						System.out.println("rs"+rs);
						while(rs.next()){
							num = rs.getString("TeaId");
							System.out.println("num"+rs);
							}
						
						if(num.length()<=0) {
							
							sql = "insert into teacher values("+TeaId+",'"+TeaName+"','"+TeaGender+"','"+TeaEmail+"','"+TeaAge+"','"+DepName+"')";
							sql2="insert into user values("+TeaId+",'"+MD5.md5("123456")+"','教师')";
;						}else {System.out.println("该账号已存在!");}
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					}else{
					//修改数据
				    sql = "update teacher set TeaName='"+TeaName+"',TeaGender='"+TeaGender+"',TeaEmail='"+TeaEmail+"',TeaAge='"+TeaAge+"',DepName='"+DepName+"' where TeaId ='"+TeaId+"'";	
				}
				System.out.println("sql="+sql);

				 int r = 0;
				try {
					if (sql.length()>0)
					r = new DBtools().execute(sql);
					if (sql2.length()>0) r = new DBtools().execute(sql2);
				} catch (ClassNotFoundException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 System.out.println("影响行数："+r); 
				 
				 
				//返回页面
				response.sendRedirect("depcontacts/tealist.jsp");
				
	}

	/**
	 * @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
	 */
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
